import pygame, sys, random
pygame.init()
pygame.key.set_repeat(100, 25)

player_speed = 7
enemy_horiz_speed = 5
enemy_vert_speed = 0

screen = pygame.display.set_mode([888,888])
screen.fill([147,180,109])

playerRect = pygame.Rect(200,300,11,29)
cpuRect= pygame.Rect(100,200,20,22)
def paintGame():
    global playerRect,cpuRect
    screen.fill([147,180,109])
    pygame.draw.rect(screen, [211,123,200], playerRect, 0)
    
    
    pygame.draw.rect(screen, [100,50,25], cpuRect, 0)
    pygame.display.flip()

def activateEnemyBrain():
    global playerRect,cpuRect, enemy_horiz_speed, enemy_vert_speed, gameIsRunning

    if cpuRect.right > screen.get_width():
        cpuRect.right = screen.get_width()
        enemy_horiz_speed = enemy_horiz_speed * -1 
    elif cpuRect.left < 0:
        cpuRect.left = 0
        enemy_horiz_speed = enemy_horiz_speed * -1 
    elif cpuRect.bottom > screen.get_height():
        cpuRect.bottom = screen.get_height()
        enemy_vert_speed = enemy_vert_speed * -1 
    elif cpuRect.top < 0: 
        cpuRect.top = 0
        enemy_vert_speed = enemy_vert_speed * -1
    elif random.randint(0,20) < 2:
        enemy_vert_speed = random.randint(-10,11)
        enemy_horiz_speed = random.randint(-10,11)
        
    if playerRect.colliderect(cpuRect):
        gameIsRunning = False
        
    cpuRect.left += enemy_horiz_speed
    cpuRect.top += enemy_vert_speed
       
    
gameIsRunning = True
while gameIsRunning:

    activateEnemyBrain()
    
    for event in  pygame.event.get():
        if event.type == pygame.QUIT:
           gameIsRunning = False
            
        elif event.type == pygame.KEYDOWN:
            
            if event.key == pygame.K_UP:
                playerRect.top -= player_speed
                if playerRect.top < 0:
                    playerRect.top = 0 
                if playerRect.colliderect(cpuRect):
                     gameIsRunning = False                
            elif event.key == pygame.K_DOWN:
                playerRect.top += player_speed
                if playerRect.bottom > screen.get_height():
                    playerRect.bottom = screen.get_height()
                if playerRect.colliderect(cpuRect):
                     gameIsRunning = False                    
            elif event.key == pygame.K_RIGHT:
                playerRect.left += player_speed
                if playerRect.right > screen.get_width():
                    playerRect.right = screen.get_width()
                if playerRect.colliderect(cpuRect):
                     gameIsRunning = False                    
            elif event.key == pygame.K_LEFT:
                playerRect.left -= player_speed
                if playerRect.left < 0:
                    playerRect.left = 0
                if playerRect.colliderect(cpuRect):
                     gameIsRunning = False
    
    paintGame()
    pygame.time.delay(20)
    
pygame.quit()
            